package com.lepu.app.fun.tool.dcb;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.app.utils.LoginUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.dcb.adapter.ToolDaiChanAdapter;
import com.lepu.app.fun.tool.dcb.bean.BeanToolDaiChan;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.ListViewForScrollview;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;

public class DaiChanActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener , OnTopbarNewRightButtonListener{
	private CustomTopBarNew topbar;
	private ScrollView scrollView;
	private LinearLayout mAddLayout;
	private EditText mNameEdit;
	private EditText mNumberEdit;
	private Button mAddButton;
	
	private ListViewForScrollview mNotListView = null;
	private ListViewForScrollview mHasListView = null; 
	
	private ArrayList<BeanToolDaiChan> mNotList = null;
	private ArrayList<BeanToolDaiChan> mHasList = null;
	
	private ToolDaiChanAdapter mNotAdapter = null;
	private ToolDaiChanAdapter mHasAdapter = null;

	private boolean isShowAdd = false;
	
	public static DaiChanActivity instance;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tool_daichan_activity);

		UtilityBase.setStatusBarRes(this, R.color.color_daichan, true);
		instance = this;
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		instance = null;
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void onTopbarRightButtonSelected() {
		if(isShowAdd){
			mAddLayout.setVisibility(View.GONE);
			isShowAdd = false;
			topbar.setRightButton(R.drawable.tool_daichan_show_icon);
		}else{
			mAddLayout.setVisibility(View.VISIBLE);
			isShowAdd = true;
			topbar.setRightButton(R.drawable.tool_daichan_hide_icon);
			
			scrollView.smoothScrollTo(0,20);
		}
	}
	
	public static DaiChanActivity getInstance(){
		return instance;
	}
	
	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("待产包");
		topbar.setRightButton(R.drawable.tool_daichan_show_icon);
		topbar.setTopbarBackground(R.color.color_daichan);
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);
		
		scrollView = (ScrollView) findViewById(R.id.scrollView);
		mAddLayout = (LinearLayout) findViewById(R.id.addLayout);
		mNameEdit = (EditText) findViewById(R.id.editTextName);
		mNumberEdit = (EditText) findViewById(R.id.editTextNumber);
		mAddButton = (Button) findViewById(R.id.addButton);
		
		mNotListView =  (ListViewForScrollview) findViewById(R.id.notListView);
		mHasListView =  (ListViewForScrollview) findViewById(R.id.hasListView);
		
		mAddButton.setOnClickListener(onClick);
		
		scrollView.smoothScrollTo(0,20);
		
		mNotList = BeanToolDaiChan.getDataByDone(LoginUtil.getUserId(), 0);
		mHasList = BeanToolDaiChan.getDataByDone(LoginUtil.getUserId(), 1);
		
		//首次添加默认数据
		if(mNotList.size() == 0 && mHasList.size() == 0){
			ArrayList<BeanToolDaiChan> list = BeanToolDaiChan.getDataByDone("", 0);
			for(int i=0; i<list.size(); i++){
				BeanToolDaiChan bean = list.get(i);
				bean.uid = LoginUtil.getUserId();
				BeanToolDaiChan.insert(bean);
			}
			
			mNotList = BeanToolDaiChan.getDataByDone(LoginUtil.getUserId(), 0);
		}
		
		mNotAdapter = new ToolDaiChanAdapter(DaiChanActivity.this, mNotList);
		mHasAdapter = new ToolDaiChanAdapter(DaiChanActivity.this, mHasList);
		
		mNotListView.setAdapter(mNotAdapter);
		mHasListView.setAdapter(mHasAdapter);
		
		//长按删除
		mNotListView.setOnItemLongClickListener(new OnItemLongClickListener() {
			
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				
				new MyDialog(DaiChanActivity.this)
				.setMessage("您确定删除吗?")
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						
					}
				})
				.setPositiveButton(R.string.app_ok,
						new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						BeanToolDaiChan.deleteItem(LoginUtil.getUserId(), mNotList.get(position).name);
						mNotList.remove(position);
						
						mNotAdapter.notifyDataSetChanged();
						
					}
				}).create(null).show();
				
				return false;//default false
			}
		});
		//长按删除
		mHasListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				
				new MyDialog(DaiChanActivity.this)
                .setMessage("您确定删除吗?")
                .setNegativeButton(R.string.app_cancel,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                            }
                        })
                .setPositiveButton(R.string.app_ok,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                            	BeanToolDaiChan.deleteItem(LoginUtil.getUserId(), mHasList.get(position).name);
                				mHasList.remove(position);
                				
                				mHasAdapter.notifyDataSetChanged();
                               
                            }
                        }).create(null).show();
				
				return false;//default false
			}
		});
	}

     
	View.OnClickListener onClick = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.addButton:
				String name = mNameEdit.getText().toString();
				String number = mNumberEdit.getText().toString();
				if(TextUtils.isEmpty(name)){
					UIHelper.showToast(DaiChanActivity.this, "请输入名称");
					return;
				}
				
//				if(TextUtils.isEmpty(number)){
//					UIHelper.showToast(DaiChanActivity.this, "请输入数量");
//					return;
//				}
				
				if(BeanToolDaiChan.isExist(name)){
					UIHelper.showToast(DaiChanActivity.this, "该名称已经存在");
					return;
				}
				
				BeanToolDaiChan bean = new BeanToolDaiChan();
				bean.uid = LoginUtil.getUserId();
				bean.name = name;
				bean.number = number;
				bean.done = 0;
				
				if(BeanToolDaiChan.insert(bean)){
					mNotList.add(bean);
					mNotAdapter.notifyDataSetChanged();
					UIHelper.showToast(DaiChanActivity.this, "保存成功");
					
					mAddLayout.setVisibility(View.GONE);
					isShowAdd = false;
					topbar.setRightButton(R.drawable.tool_daichan_show_icon);
					
					mNameEdit.setText("");
					mNumberEdit.setText("");
					closeEdit();
				}
				
				break;

			default:
				break;
			}
		}
	};
	
	//添加完成
    public void addItem(BeanToolDaiChan bean, int position){
    	mNotList.remove(position);
    	mHasList.add(bean);
    	
    	mNotAdapter.notifyDataSetChanged();
    	mHasAdapter.notifyDataSetChanged();
    }
	
    //删除完成
    public void removeItem(BeanToolDaiChan bean, int position){
    	mHasList.remove(position);
    	mNotList.add(bean);
    	
    	mNotAdapter.notifyDataSetChanged();
    	mHasAdapter.notifyDataSetChanged();
    }
    
    //隐藏键盘
	public void closeEdit(){
		  View view = getWindow().peekDecorView();
	        if (view != null) {
	            InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
	        }
	}
}
